#include "Server.h"

#define FDS_MAX(FD_1 , FD_2) FD_1 > FD_2 ? FD_1 :FD_2

#define LIN_MAX 3 

int main(int argc, char const *argv[])
{
    if (argc != 2 )
    {
        printf("输入的格式错误 %s port\n",argv[0]);
        exit(1);
    }
    
    int sock_client;
    int sock_server;

    int sock_client_arr[LIN_MAX] = {0};
    int sock_client_len = 0;

    char buf_recv[1024];
    char buf_send[1024] = "恭喜你,通讯成功,我已收到\n";
    char buf_recv_len = 0;

    int fds_num = 0;

    sock_server = tcp_server_init(atoi(argv[1]));

    if (-1 == listen(sock_server ,LIN_MAX))
    {
        ERRLOG("监听失败");
        close(sock_server);
        return -3;
    }
    
    fd_set Tcp_client_fds;
    fd_set Tcp_client_fds_temp;

    unsigned int Tcp_fds_max = 0;

    FD_ZERO(&Tcp_client_fds);

    FD_SET(sock_server , &Tcp_client_fds);

    Tcp_fds_max = FDS_MAX( sock_server , Tcp_fds_max);

    while (1)
    {
        FD_ZERO(&Tcp_client_fds_temp);

        Tcp_client_fds_temp = Tcp_client_fds;

        fds_num = select(Tcp_fds_max + 1 ,&Tcp_client_fds_temp,NULL ,NULL ,NULL);

        if (fds_num == -1)
        {
            printf("select 错误\n");
            continue;
        }
        
        if (FD_ISSET(sock_server , &Tcp_client_fds_temp))
        {
            sock_client = accept(sock_server , NULL , NULL);
            if (sock_client == -1)
            {
                ERRLOG("连接失败");
                return 0;
            }
            

            if (sock_client_len >= LIN_MAX )
            {
                send(sock_client ,"超过客户端最大连接数量",strlen("超过客户端最大连接数量"), 0);
                close(sock_client);
                printf("超过客户端最大连接数量\n");
                continue;
            }
            
            sock_client_arr[sock_client_len++] = sock_client;

            printf("连接成功 套接字 : %d max = %d\n",sock_client,Tcp_fds_max);

            FD_SET(sock_client , &Tcp_client_fds);

            Tcp_fds_max = FDS_MAX(sock_client , Tcp_fds_max);

        }
        
        else
        {
            for (size_t i = 0; i < sock_client_len &&fds_num != 0; i++)
            {
                if (FD_ISSET(sock_client_arr[i], &Tcp_client_fds_temp))
                {
                    fds_num--;

                    memset(buf_recv , 0 , sizeof(buf_recv));

                    buf_recv_len = recv(sock_client_arr[i],buf_recv ,sizeof(buf_recv), 0 );

                    if (buf_recv_len == 0)
                    {
                        FD_CLR(sock_client_arr[i], &Tcp_client_fds);

                        if (sock_client_arr[i] == Tcp_fds_max)
                        {
                            Tcp_fds_max--;
                        }
                        close(sock_client_arr[i]);
                        continue;
                    }
                    

                    printf("接收信息:%s\n",buf_recv);

                    send(sock_client_arr[i],buf_send,strlen(buf_send) , 0);
                }
                
            }
            
        }
    }
    

    return 0;
}
